package com.ruoyi.web.controller.device;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Dc3Device;
import com.ruoyi.system.domain.Dc3DeviceData;
import com.ruoyi.system.domain.mongodb.PointValue;
import com.ruoyi.system.service.IDc3DeviceDataService;
import com.ruoyi.system.service.IDc3DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/dc3/device/data")
public class Dc3DeviceController  extends BaseController {




    @Autowired
    private IDc3DeviceService deviceService;

    @Resource
    private IDc3DeviceDataService deviceDataService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dc3Device dc3Device)
    {
        startPage();
        List<Dc3Device> list = deviceService.selectDc3DeviceList(dc3Device);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dc3Device dc3Device)
    {
        List<Dc3Device> list = deviceService.selectDc3DeviceList(dc3Device);
        ExcelUtil<Dc3Device> util = new ExcelUtil<>(Dc3Device.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(deviceService.selectDc3DeviceById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:data:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dc3Device dc3Device)
    {
        return toAjax(deviceService.insertDc3Device(dc3Device));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:data:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dc3Device dc3Device)
    {
        return toAjax(deviceService.updateDc3Device(dc3Device));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:data:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(deviceService.deleteDc3DeviceByIds(ids));
    }




/**
     * 根据设备名称获取设备最新的状态数据
     *
     * @param dc3Device
     * @return
     */
    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/by/name")
    public AjaxResult getDc3DevicePointValue(Dc3Device dc3Device){
        JSONObject jsonObject = new JSONObject();

        //获取当前设备的最新状态数据
        List<PointValue> list = deviceService.getDc3DevicePointValue(dc3Device);
        jsonObject.put("pointValue", list);

        //获取设备的数据
        Dc3DeviceData dc3DeviceData = deviceDataService.selectDc3DeviceDataById(dc3Device.getId());
        jsonObject.put("device", dc3DeviceData);

        return success(jsonObject);
    }





}
